# -*- coding: utf-8 -*-
from scpy.logger import get_logger
import os
import sys
from config import *
from pymongo import MongoClient


reload(sys)
sys.setdefaultencoding('utf-8')

logger = get_logger(__file__)

CURRENT_PATH = os.path.dirname(__file__)
if CURRENT_PATH:
    CURRENT_PATH = CURRENT_PATH + "/"

Mongo = MongoClient(MONGO_HOST,MONGO_PORT)
MONGO_DB_WEB = 'webDB'
MONGO_COLL_COMMENT = 'comment'

def initial_db_collection():
    db = Mongo[MONGO_DB_WEB][MONGO_COLL_COMMENT]


def find_all_comment():
    db = Mongo[MONGO_DB_WEB][MONGO_COLL_COMMENT]
    commentList = list()
    count = 0
    for item in db.find().batch_size(10):
        count += 1
        if item.get('title'):
            commentList.append(item)
    return commentList


def insert_comment(title,text):
    insertDic = {'title':title,'text':text}
    try:
        db = Mongo[MONGO_DB_WEB][MONGO_COLL_COMMENT]
        db.insert(insertDic)
        return True
    except:
        return False
